package window;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import lombok.Getter;
import utils.AppUnits;

@Getter
public class WcsWindow extends JFrame {

	private static final long serialVersionUID = 1L;
	private JTextArea textArea = null;// 主要信息打印入口
	private JCheckBox chkLog = null;// 写入日志的单选框，用来查看写入日志是否选中。
	private JLabel status = null;// 状态栏打印信息入口
	
	private JTabbedPane tabbedPane;
	
    private ClassLoader cl = getClass().getClassLoader();
    public ImageIcon logo = new ImageIcon(this.cl.getResource("icons/logo.gif"));
    public ImageIcon ball = new ImageIcon(this.cl.getResource("icons/ball.gif"));
  
	public WcsWindow() {
		init();
	}

	/**
	 * 初始化
	 */
	public void init() {
	    Container localContainer = getContentPane();
	    this.tabbedPane = new JTabbedPane(1);
//	    SocketTestClient localSocketTestClient = new SocketTestClient(this);
//	    SocketTestServer localSocketTestServer = new SocketTestServer(this);
//	    SocketTestUdp localSocketTestUdp = new SocketTestUdp(this);
	    About localAbout = new About();
//	    this.tabbedPane.addTab("Client", this.ball, localSocketTestClient, "Test any server");
//	    this.tabbedPane.addTab("Server", this.ball, localSocketTestServer, "Test any client");
//	    this.tabbedPane.addTab("Udp", this.ball, localSocketTestUdp, "Test any UDP Client or Server");
	    this.tabbedPane.addTab("About", this.ball, localAbout, "About SocketTest");
	    this.tabbedPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
	    localContainer.add(this.tabbedPane);
		setBase();
		setText();
		setToolBar();
		setStatusBar();
		end();
	}

	/**
	 * ps:一些窗体的基本设置
	 */
	public void setBase() {

		this.setTitle("WCS_SOCKET"); // 对窗口设置标题
		this.setSize(900, 600); // 设置窗口的大小
		this.setLocationRelativeTo(null); // 设置窗口居中
		// 设置窗口关闭的后的动作
		// DO_NOTHING_ON_CLOSE: 表示什么都不做，通过代码处理
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		// 主题设置
		try {
			String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
			UIManager.setLookAndFeel(lookAndFeel);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 监控关闭按钮,添加关闭前的提示信息
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				if (JOptionPane.showConfirmDialog(null, "要关闭吗?", "提示信息",
						JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) {
					System.exit(0);
				}
			}
		});
	}

	/**
	 * ps:内容设置
	 */
	public void setText() {
		textArea = new JTextArea();// 注意需要在new JScrollPane(textArea)之前赋值，不然无法显示
		JScrollPane jScrollPaneInfo = new JScrollPane(textArea);
//		textArea.setLineWrap(false); //是否换行:true表示会换行；false表示不换行。默认为false。
//		textArea.setWrapStyleWord(true); //换行策略,setLineWrap()为true才有意义
//		textArea.setText("");
		this.add(jScrollPaneInfo);
	}

	/**
	 * ps:工具栏设置
	 */
	public void setToolBar() {
		JToolBar toolBar = new JToolBar("工具栏");// 创建工具栏对象
		toolBar.setFloatable(false); // 设置为不允许拖动
		this.add(toolBar, BorderLayout.NORTH);// 设置位置在顶部
		// 清日志
		final JButton clearButton = new JButton("清日志");// 创建按钮对象
		clearButton.addActionListener(new ButtonListener()); // 添加动作事件监听器
		toolBar.add(clearButton); // 添加到工具栏中
		toolBar.addSeparator(); // 添加默认大小的分隔符
		// 写入日志
		chkLog = new JCheckBox("写入log日志");
		chkLog.addActionListener(new ButtonListener());
		chkLog.setSelected(true);
		toolBar.add(chkLog);
		toolBar.addSeparator(); // 添加指定大小的分隔符
		// 重启
		final JButton resetButton = new JButton("重启"); // 创建按钮对象
		resetButton.addActionListener(new ButtonListener()); // 添加动作事件监听器
		toolBar.add(resetButton); // 添加到工具栏中
		toolBar.addSeparator(); // 添加默认大小的分隔符

	}

	/**
	 * ps:状态栏设置
	 */
	public void setStatusBar() {
		JPanel statusBar = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 状态栏
		statusBar.setBorder(new CompoundBorder(new LineBorder(Color.DARK_GRAY), new EmptyBorder(4, 4, 4, 4)));
		status = new JLabel();
		status.setText("等待数据库连接！");
//		JLabel status2 = new JLabel();
//		status2.setText("444");
		statusBar.add(status);
//		statusBar.add(status2);
		add(statusBar, BorderLayout.SOUTH);

	}

	/**
	 * 监听事件
	 *
	 */
	private class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			// 类型检查
			if (e.getSource() instanceof JButton) {
				JButton button = (JButton) e.getSource();
				if (button.getText() == "清日志") {
					textArea.setText("");
				} else if (button.getText() == "重启") {
					if (JOptionPane.showConfirmDialog(null, "要重启吗?", "提示信息",
							JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) {
						String path = getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
//						toolsFunc.restartApplication(System.getProperty("java.class.path"));
						AppUnits.restartApplication(path);
					}
				}
			} else if (e.getSource() instanceof JCheckBox) {
				JCheckBox checkBox = (JCheckBox) e.getSource();
				if (checkBox.getText() == "写入log日志") {

					if (checkBox.isSelected()) {
						textArea.append("开始写入日志。。。。。。\n");
					} else {
						textArea.append("停止写入日志。。。。。。\n");
					}
				}
			}
		}
	}

	/**
	 * ps:需要放到最后加载的代码
	 */
	public void end() {
		this.setVisible(true); // 设置窗口显示,不然无界面;不放在最后还会导致状态栏加载不出来。
	}

	public static void main(String[] args) {

		System.out.println("打开窗口。。。。。。。");
		WcsWindow window = new WcsWindow();
		window.textArea.append("hello\n");
		window.textArea.append("<html><div>test</div></html>");
		window.status.setText("<html><body><div><span style='color:blue'>gie</span></div></body></html>");

	}

}
